package com.yaofeng.mybatis.modules.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaofeng.mybatis.annotation.Log;
import com.yaofeng.mybatis.common.PageInfo;
import com.yaofeng.mybatis.common.ResponseInfo;
import com.yaofeng.mybatis.modules.sys.entity.Post;
import com.yaofeng.mybatis.modules.sys.service.IPostService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author yaofeng
 * @since 2019-10-31
 */
@RestController
@RequestMapping("/sys/post")
public class PostController {

    @Autowired
    private IPostService  iPostService;

    /**
     * 查看岗位列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @RequiresAuthentication
    @Log(value = "岗位列表")
    public ResponseInfo list(@RequestParam Map<String, Object> params, @RequestParam(defaultValue = "1") Long pageNum, @RequestParam(defaultValue = "50") Long pageSize) {
        IPage page = iPostService.page(new Page<>(pageNum, pageSize),
                Wrappers.<Post>lambdaQuery()
                        .eq(StringUtils.isNotEmpty(params.get("postName").toString()),Post::getPostName,params.get("postName").toString())
                        .eq(StringUtils.isNotEmpty(params.get("status").toString()),Post::getPostName,params.get("status").toString()));
        return ResponseInfo.success(new PageInfo(page));
    }

    @GetMapping("/select/list")
    @RequiresAuthentication
    @Log(value = "岗位下拉列表")
    public ResponseInfo selectList() {
        List<Post>  postList  =   iPostService.list();
        return ResponseInfo.success(postList);
    }


    /**
     * 保存岗位
     * @param post
     * @return
     */
    @PostMapping("/save")
    @RequiresAuthentication
    @Log(value = "保存岗位")
    public  ResponseInfo save(@RequestBody  Post  post){
        iPostService.save(post);
        return ResponseInfo.success();
    }

    /**
     * 更新岗位
     * @param post
     * @return
     */
    @PutMapping("/update")
    @RequiresAuthentication
    @Log(value = "更新岗位")
    public  ResponseInfo update(@RequestBody Post  post){
        iPostService.updateById(post);
        return ResponseInfo.success();
    }


    /**
     * 删除岗位
     * @param id
     * @return
     */
    @DeleteMapping ("/delete")
    @RequiresAuthentication
    @Log(value = "删除岗位")
    public  ResponseInfo delete(Long id){
        iPostService.removeById(id);
        return ResponseInfo.success();
    }

}
